﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;

namespace VsSpeakerCompanion.UI
{
    internal class WidgetsControlPresenter : IWidget
    {
        private const double DefaultPaddingBetweenWidgets = 5;

        private Context context;
        private WidgetsControl view;
        private List<IWidget> widgets = new List<IWidget>();

        internal WidgetsControlPresenter(Context context)
        {
            this.context = context;
            this.view = new WidgetsControl();
        }

        internal void Add(IWidget widget)
        {
            if (!this.widgets.Contains(widget))
            {
                this.widgets.Add(widget);

                if (widget.VisualElement != null)
                {
                    this.view.Add(widget);

                    // Set the default padding to the widget (so it will be separated from the other widgets)
                    widget.VisualElement.SetValue(Control.PaddingProperty, new Thickness(DefaultPaddingBetweenWidgets));
                    // Subscribe to the widget visual element events
                    widget.VisualElement.MouseDown += new MouseButtonEventHandler(OnWidgetVisualElementMouseDown);
                    widget.VisualElement.MouseMove += new MouseEventHandler(OnWidgetVisualElementMouseMove);
                    widget.VisualElement.KeyDown += new KeyEventHandler(OnwidgetVisualElementKeyDown);
                    widget.VisualElement.KeyUp += new KeyEventHandler(OnwidgetVisualElementKeyUp);
                }
            }
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public void Reset()
        {
            this.widgets.ForEach(widget => widget.Reset());
        }

        /// <summary>
        /// See <see cref="IWidget"/> for more information
        /// </summary>
        public System.Windows.UIElement VisualElement
        {
            get { return this.view; }
        }

        private void OnwidgetVisualElementKeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void OnwidgetVisualElementKeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;
        }

        private void OnWidgetVisualElementMouseDown(object sender, MouseButtonEventArgs e)
        {
            e.Handled = true;
        }

        private void OnWidgetVisualElementMouseMove(object sender, MouseEventArgs e)
        {
            e.Handled = true;
        }
    }
}